1 /* 2 * Copyright (C) 2007 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.common.collect; 18 19 import static com.google.common.base.Preconditions.checkPositionIndexes; 20 21 import com.google.common.annotations.GwtCompatible; 22 import com.google.common.annotations.GwtIncompatible; 23 24 import java.lang.reflect.Array; 25 import java.util.Collection; 26 27 import javax.annotation.Nullable; 28 29 /** 30 * Static utility methods pertaining to object arrays. 31 * 32 * @author Kevin Bourrillion 33 * @since 2.0 (imported from Google Collections Library) 34 */ 35 @GwtCompatible(emulated = true) 36 public final class ObjectArrays { 37 static final Object[] EMPTY_ARRAY = new Object[0]; 38 39 private ObjectArrays() {} 40 41 /** 42 * Returns a new array of the given length with the specified component type. 43 * 44 * @param type the component type 45 * @param length the length of the new array 46 */ 47 @GwtIncompatible("Array.newInstance(Class, int)") 48 @SuppressWarnings("unchecked") 49 public static <T> T[] newArray(Class<T> type, int length) { 50 return (T[]) Array.newInstance(type, length); 51 } 52 53 /** 54 * Returns a new array of the given length with the same type as a reference 55 * array. 56 * 57 * @param reference any array of the desired type 58 * @param length the length of the new array 59 */ 60 public static <T> T[] newArray(T[] reference, int length) { 61 return Platform.newArray(reference, length); 62 } 63 64 /** 65 * Returns a new array that contains the concatenated contents of two arrays. 66 * 67 * @param first the first array of elements to concatenate 68 * @param second the second array of elements to concatenate 69 * @param type the component type of the returned array 70 */ 71 @GwtIncompatible("Array.newInstance(Class, int)") 72 public static <T> T[] concat(T[] first, T[] second, Class<T> type) { 73 T[] result = newArray(type, first.length + second.length); 74 System.arraycopy(first, 0, result, 0, first.length); 75 System.arraycopy(second, 0, result, first.length, second.length); 76 return result; 77 } 78 79 /** 80 * Returns a new array that prepends {@code element} to {@code array}. 81 * 82 * @param element the element to prepend to the front of {@code array} 83 * @param array the array of elements to append 84 * @return an array whose size is one larger than {@code array}, with 85 * {@code element} occupying the first position, and the 86 * elements of {@code array} occupying the remaining elements. 87 */ 88 public static <T> T[] concat(@Nullable T element, T[] array) { 89 T[] result = newArray(array, array.length + 1); 90 result[0] = element; 91 System.arraycopy(array, 0, result, 1, array.length); 92 return result; 93 } 94 95 /** 96 * Returns a new array that appends {@code element} to {@code array}. 97 * 98 * @param array the array of elements to prepend 99 * @param element the element to append to the end 100 * @return an array whose size is one larger than {@code array}, with 101 * the same contents as {@code array}, plus {@code element} occupying the 102 * last position. 103 */ 104 public static <T> T[] concat(T[] array, @Nullable T element) { 105 T[] result = arraysCopyOf(array, array.length + 1); 106 result[array.length] = element; 107 return result; 108 } 109 110 /** GWT safe version of Arrays.copyOf. */ 111 static <T> T[] arraysCopyOf(T[] original, int newLength) { 112 T[] copy = newArray(original, newLength); 113 System.arraycopy( 114 original, 0, copy, 0, Math.min(original.length, newLength)); 115 return copy; 116 } 117 118 /** 119 * Returns an array containing all of the elements in the specified 120 * collection; the runtime type of the returned array is that of the specified 121 * array. If the collection fits in the specified array, it is returned 122 * therein. Otherwise, a new array is allocated with the runtime type of the 123 * specified array and the size of the specified collection. 124 * 125 * <p>If the collection fits in the specified array with room to spare (i.e., 126 * the array has more elements than the collection), the element in the array 127 * immediately following the end of the collection is set to {@code null}. 128 * This is useful in determining the length of the collection <i>only</i> if 129 * the caller knows that the collection does not contain any null elements. 130 * 131 * <p>This method returns the elements in the order they are returned by the 132 * collection's iterator. 133 * 134 * <p>TODO(kevinb): support concurrently modified collections? 135 * 136 * @param c the collection for which to return an array of elements 137 * @param array the array in which to place the collection elements 138 * @throws ArrayStoreException if the runtime type of the specified array is 139 * not a supertype of the runtime type of every element in the specified 140 * collection 141 */ 142 static <T> T[] toArrayImpl(Collection<?> c, T[] array) { 143 int size = c.size(); 144 if (array.length < size) { 145 array = newArray(array, size); 146 } 147 fillArray(c, array); 148 if (array.length > size) { 149 array[size] = null; 150 } 151 return array; 152 } 153 154 /** 155 * Implementation of {@link Collection#toArray(Object[])} for collections backed by an object 156 * array. the runtime type of the returned array is that of the specified array. If the collection 157 * fits in the specified array, it is returned therein. Otherwise, a new array is allocated with 158 * the runtime type of the specified array and the size of the specified collection. 159 * 160 * <p>If the collection fits in the specified array with room to spare (i.e., the array has more 161 * elements than the collection), the element in the array immediately following the end of the 162 * collection is set to {@code null}. This is useful in determining the length of the collection 163 * <i>only</i> if the caller knows that the collection does not contain any null elements. 164 */ 165 static <T> T[] toArrayImpl(Object[] src, int offset, int len, T[] dst) { 166 checkPositionIndexes(offset, offset + len, src.length); 167 if (dst.length < len) { 168 dst = newArray(dst, len); 169 } else if (dst.length > len) { 170 dst[len] = null; 171 } 172 System.arraycopy(src, offset, dst, 0, len); 173 return dst; 174 } 175 176 /** 177 * Returns an array containing all of the elements in the specified 178 * collection. This method returns the elements in the order they are returned 179 * by the collection's iterator. The returned array is "safe" in that no 180 * references to it are maintained by the collection. The caller is thus free 181 * to modify the returned array. 182 * 183 * <p>This method assumes that the collection size doesn't change while the 184 * method is running. 185 * 186 * <p>TODO(kevinb): support concurrently modified collections? 187 * 188 * @param c the collection for which to return an array of elements 189 */ 190 static Object[] toArrayImpl(Collection<?> c) { 191 return fillArray(c, new Object[c.size()]); 192 } 193 194 /** 195 * Returns a copy of the specified subrange of the specified array that is literally an Object[], 196 * and not e.g. a {@code String[]}. 197 */ 198 static Object[] copyAsObjectArray(Object[] elements, int offset, int length) { 199 checkPositionIndexes(offset, offset + length, elements.length); 200 if (length == 0) { 201 return EMPTY_ARRAY; 202 } 203 Object[] result = new Object[length]; 204 System.arraycopy(elements, offset, result, 0, length); 205 return result; 206 } 207 208 private static Object[] fillArray(Iterable<?> elements, Object[] array) { 209 int i = 0; 210 for (Object element : elements) { 211 array[i++] = element; 212 } 213 return array; 214 } 215 216 /** 217 * Swaps {@code array[i]} with {@code array[j]}. 218 */ 219 static void swap(Object[] array, int i, int j) { 220 Object temp = array[i]; 221 array[i] = array[j]; 222 array[j] = temp; 223 } 224 225 static Object[] checkElementsNotNull(Object... array) { 226 return checkElementsNotNull(array, array.length); 227 } 228 229 static Object[] checkElementsNotNull(Object[] array, int length) { 230 for (int i = 0; i < length; i++) { 231 checkElementNotNull(array[i], i); 232 } 233 return array; 234 } 235 236 // We do this instead of Preconditions.checkNotNull to save boxing and array 237 // creation cost. 238 static Object checkElementNotNull(Object element, int index) { 239 if (element == null) { 240 throw new NullPointerException("at index " + index); 241 } 242 return element; 243 } 244 }